package com.mud.mudcommune.view.fragment.threeLevel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.ArticleEntry;
import com.mud.mudcommune.entry.ArticleResponse;
import com.mud.mudcommune.http.ApiService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by Administrator on 2017/2/21 0021.
 */

public class ArticleDetailsFragment extends Fragment implements Callback<ArticleResponse> {
    private static final String TAG = ArticleDetailsFragment.class.getSimpleName();
    private static final Fragment fragment = new ArticleDetailsFragment();
    @BindView(R.id.article_title)
    TextView title;
    @BindView(R.id.article_img)
    ImageView articleImage;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.back)
    ImageView back;
    private Unbinder unbind;

    public static Fragment getInstance() {
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_article_detials, container, false);
        unbind = ButterKnife.bind(this, view);
        requestData();
        return view;
    }

    @OnClick({R.id.back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                getFragmentManager().popBackStack();
                break;
        }
    }

    private void requestData() {
        Bundle arguments = fragment.getArguments();
        int articleId = arguments.getInt("article_id");
        Retrofit retrofit = new Retrofit.Builder().baseUrl(ApiService.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        Call<ArticleResponse> call = apiService.getArticleDetail(articleId);
        call.enqueue(this);
    }

    @Override
    public void onResponse(Call<ArticleResponse> call, Response<ArticleResponse> response) {
        ArticleResponse body = response.body();
        if (body.getArticleList() != null && body.getArticleList().size() > 0) {
            ArticleEntry articleEntry = body.getArticleList().get(0);
            String title = articleEntry.getTitle();
            if (title != null) {
                this.title.setText(title);
            }
            Glide.with(getContext())
                    .load(articleEntry.getArticleImg())
                    .into(articleImage);
            content.setText(articleEntry.getContent());
        }

    }

    @Override
    public void onFailure(Call<ArticleResponse> call, Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbind.unbind();
    }
}
